(function ($,layer) {
    $.fn.extend({
        /**
         * @param option
         * {
         *  callback:function(res,fileInputer,inputer,preImg){
         *
         *  }
         * }
         */
		fileInputer:function (option) {
            var tgs = $(this);

            if(!tgs || tgs.length == 0){
                alert("没有FileInputControl");
            }
            $(tgs).each(function(){
                var tg = $(this);
                var filer = $(tg).find("input[type='file']");
                if(filer && filer.length > 0){
                    var fileObj = filer[0];

                    var preImg = $(this).attr("previd"),preImgCon;
                    if(preImg){
                        preImgCon = $("#" + preImg);
                    }else{
                        if(tg.find("img").length > 0){
                            preImgCon = tg.find("img")[0];
                        }
                    }

                    buttonEvents(tg, $(fileObj), preImgCon);

                    $(fileObj).on("change",function () {
                        var obj = this;
                        if(preImgCon){
                            if($(obj).val()){
                                imgView(obj, preImgCon);
                            }else{
                                $(preImgCon).prop("src","");
                            }
                        }
                    });
                }
            });

            function buttonEvents(tgCon,inputer,preImg) {
                $(tgCon).find("button").on("click",function () {
                    var tg = $(this).attr("tg");
                    if(tg == "submit"){
                        if($(inputer).val()){
                            var forms = $(tgCon).find("form");
                            if(forms){
                                var formData = new FormData(forms[0]);
                                var action = $(forms[0]).prop("action");
                                $.ajax({
                                    url:action,
                                    method: 'POST',
                                    data: formData,
                                    contentType: false,
                                    processData: false,
                                    success: function (resp) {
                                        alert(resp.message);
                                        forms[0].reset();
                                        if(resp.success){
                                            if(option && option.callback){
                                                option.callback.call(null, resp,tgCon,inputer,preImg);
                                            }else{
                                                reset(tgCon,inputer,preImg);
                                            }
                                        }
                                    }
                                });
                            }
                        }

                    }
                    else if(tg == "cancel"){
                        reset(tgCon,inputer,preImg);
                    }
                });
            }
            
            function reset(tgCon,inputer,preImg) {
                $(inputer).val("");
                preImg?$(preImg).prop("src",""):void(0);
                $(tgCon).find("a.close").click();
                var orgImg = $(tgCon).attr("orgimgid");
                if(orgImg){
                    var orgImgSrc = $("#" + orgImg).prop("src");
                    if(orgImgSrc.indexOf("?")>=0){
                        orgImgSrc += ("" + Math.floor(Math.random()*10)) ;
                    }else{
                        orgImgSrc += ("?" + Math.floor(Math.random()*10)) ;
                    }
                    $("#" + orgImg).prop("src",orgImgSrc)
                }
            }
            
            function imgView(fileTg,imgView){
                if(!fileTg || !imgView){
                    return;
                }
                if(fileTg.files && fileTg.files[0]){
                    imgView.src = window.URL.createObjectURL(fileTg.files[0]);
                }else{
                    imgView.src = fileTg.files[0].value;
                }
            };
        }
    });
})(jQuery);